;
; NOTER 
; A simple time displaying tool
; Displays time with a specified difference to your local time
; 
; Copyright (C) 2007 Sandesh Singh (http://sandesh247.googlepages.com/)
; 
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License as
; published by the Free Software Foundation; either version 2 of the
; License, or (at your option) any later version.

; This program is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
; General Public License for more details.

; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
; USA


build=1.1
dateRIniFilepath=dateR.ini

GoSub ReadDateRSettings
GoSub CreateDateRGui

menu, tray, add, Settings, ShowDateRSettings
menu, tray, add, Open Website, OpenDateRWebsite
menu, tray, add, About DateR, AboutDateR

DateR:
SetTitleMatchMode 3
ifWinExist dateR
    Gui, Hide
else
    Gui, Show, NoActivate, dateR
return

CreateDateRGui:
    Gui, Color, %backColor%, %backColor%
    Gui, Margin, 1, 1
    Gui, Font, w700
    
    GoSub UpdateDateR ; This call ensures *almost* correct width of the window
    Gui, Add, Text, vMyTime c%foreColor%, %MyTime%
    
    SetTimer, UpdateDateR, %updateInterval%
    
    if setBorder
        Gui -Caption +LastFound ToolWindow Border
    else
        Gui -Caption +LastFound ToolWindow
        
    Gui, Show, NoActivate, dateR
    
    if displayOSD
        WinSet, Transcolor, %backColor%
    else
        WinSet, Transparent, %setTransparent%

    SysGet, mon, MonitorWorkArea
    WinGetPos, , , Width, Height

    WinMove, (monRight - Width - clearRight), (monTop + clearTop)
    WinSet AlwaysOnTop, on
    
    Gui, Hide
return


UpdateDateR:
    dispTimeStamp = ; Blank it out for current time
    dispTimeStamp += timeDiff, %timeDiffType%
    FormatTime dispTimeStr, %dispTimeStamp%, %dispFormat%
    MyTime = %timeLabel% %dispTimeStr%
    GuiControl,, MyTime, %MyTime%
return


WriteDateRSettings:
    IniWrite, %build%, %dateRIniFilepath%, Settings, build
    IniWrite, %shortcutKey%, %dateRIniFilepath%, Settings, shortcutKey
    IniWrite, %clearRight%, %dateRIniFilepath%, Settings, clearRight
    IniWrite, %clearTop%, %dateRIniFilepath%, Settings, clearTop
    IniWrite, %backColor%, %dateRIniFilepath%, Settings, backColor
    IniWrite, %foreColor%, %dateRIniFilepath%, Settings, foreColor
    IniWrite, %timeDiff%, %dateRIniFilepath%, Settings, timeDiff
    IniWrite, %timeDiffType%, %dateRIniFilepath%, Settings, timeDiffType
    IniWrite, %timeLabel%, %dateRIniFilepath%, Settings, timeLabel
    IniWrite, %dispFormat%, %dateRIniFilepath%, Settings, dispFormat
    IniWrite, %updateInterval%, %dateRIniFilepath%, Settings, updateInterval
    IniWrite, %setBorder%, %dateRIniFilepath%, Settings, setBorder
    IniWrite, %setTransparent%, %dateRIniFilepath%, Settings, setTransparent
    IniWrite, %displayOSD%, %dateRIniFilepath%, Settings, displayOSD
return


ReadDateRSettings:
    IfNotExist, %dateRIniFilepath%
    {
        GoSub SetDefaultDateRSettings
        GoSub WriteDateRSettings
    }
    
    IniRead, iniBuild, %dateRIniFilepath%, Settings, build
    If (iniBuild < build)
    {
        GoSub SetDefaultDateRSettings
        GoSub WriteDateRSettings
    }    
    
    IniRead, shortcutKey, %dateRIniFilepath%, Settings, shortcutKey
    IniRead, clearRight, %dateRIniFilepath%, Settings, clearRight
    IniRead, clearTop, %dateRIniFilepath%, Settings, clearTop
    IniRead, backColor, %dateRIniFilepath%, Settings, backColor
    IniRead, foreColor, %dateRIniFilepath%, Settings, foreColor
    IniRead, timeDiff, %dateRIniFilepath%, Settings, timeDiff
    IniRead, timeDiffType, %dateRIniFilepath%, Settings, timeDiffType
    IniRead, timeLabel, %dateRIniFilepath%, Settings, timeLabel
    IniRead, dispFormat, %dateRIniFilepath%, Settings, dispFormat
    IniRead, updateInterval, %dateRIniFilepath%, Settings, updateInterval
    IniRead, setBorder, %dateRIniFilepath%, Settings, setBorder
    IniRead, setTransparent, %dateRIniFilepath%, Settings, setTransparent
    IniRead, displayOSD, %dateRIniFilepath%, Settings, displayOSD
    
    HotKey,%shortcutKey%, DateR
    ;HotKey,%shortcutKey%, On
    ;Send %shortcutKey%
return

SetDefaultDateRSettings:
    shortcutKey := "^!\"

    clearRight = 80
    clearTop = 0
    backColor = 3A6EA5
    foreColor = FFFFFF
    timeDiff = -3.5
    timeDiffType := "Hours"
    timeLabel := "Remote Time:"
    dispFormat = hh:mm:ss tt
    updateInterval = 500
    setBorder := true
    setTransparent := 255
    displayOSD := false
return


ShowDateRSettings:
Gui, 2:Destroy
HotKey,%shortcutKey%, Off

Gui, 2:Add, Text, +Right, Shortcut (hide/unhide)
Gui, 2:Add, Text, +Right, Distance from right
Gui, 2:Add, Text, +Right, Distance from top
Gui, 2:Add, Text, +Right, Background color
Gui, 2:Add, Text, +Right, Foreground color
Gui, 2:Add, Text, +Right, Time difference
Gui, 2:Add, Text, +Right, Unit of time difference
Gui, 2:Add, Text, +Right, Label
Gui, 2:Add, Text, +Right, Display format
Gui, 2:Add, Text, , Update Interval (ms)
Gui, 2:Add, Text, , Opacity Level
Gui, 2:Add, CheckBox, yp+40 vsetBorder Checked%setBorder% , Border

Gui, 2:Add, Hotkey, ym vshortcutKey, %shortcutKey%
Gui, 2:Add, Edit, +Number vclearRight wp, %clearRight%
Gui, 2:Add, Edit, +Number vclearTop wp, %clearTop%
Gui, 2:Add, Edit, vbackColor wp, %backColor%
Gui, 2:Add, Edit, vforeColor wp, %foreColor%
Gui, 2:Add, Edit, vtimeDiff wp, %timeDiff%
Gui, 2:Add, Edit, vtimeDiffType wp, %timeDiffType%
Gui, 2:Add, Edit, vtimeLabel wp, %timeLabel%
Gui, 2:Add, Edit, vdispFormat wp, %dispFormat%
Gui, 2:Add, Edit, +Number vupdateInterval wp, %updateInterval%
Gui, 2:Add, Slider, vsetTransparent yp+30 Range0-255, %setTransparent%
Gui, 2:Add, CheckBox, xm vdisplayOSD Checked%displayOSD% , OSD display

Gui, 2:Add, Button, xm w50 yp+30 gAcceptDateRSettings, Ok 
Gui, 2:Add, Button, xp+70 wp gDiscardDateRSettings, Cancel
Gui, 2:Show, , dateR Settings
Return

AcceptDateRSettings:
    Gui, 2:Submit
    GoSub WriteDateRSettings
    Reload
Return

DiscardDateRSettings:
    Gui, 2:Destroy
Return

AboutDateR:
    MsgBox dateR: A remote time display tool. By Sandesh Singh (http://sandesh247.googlepages.com/) 
return

OpenDateRWebsite:
    Run http://sandesh247.googlepages.com/autohotkeyscripts
return
